home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
et
/
et3_0-a1.lha
/
et3
/
src
/
Text.h
< prev
next >
Wrap
C/C++ Source or Header
|
1992-08-26
|
8KB
|
272 lines
#ifndef Text_First
#ifdef __GNUG__
//pragma once
#pragma interface
#endif
#define Text_First
#include "Object.h"
#include "String.h"
#include "Font.h"
#include "Port.h"
#include "Storage.h"
#include "TextStyles.h"
#include "TextFormatter.h"
#include "Mark.h"
class RegularExp;
//---- VisualMark ------------------------------------------------------
// VisualMarks are objects with a visual representation. They
// can be inserted in a text and behave like characters.
class VisualMark: public Mark {
public:
MetaDef(VisualMark);
VisualMark();
virtual void CalcExtent();
virtual Point GetExtent();
virtual int Base();
virtual void Draw(Point at, Rectangle clip, Rectangle line, bool border);
virtual void SendDown(int, int, void *);
virtual void SetContainer(class VObject *);
virtual bool WantsInput(Point lp);
virtual class Command *Input(Point lp, Token &t, class Clipper *cl);
virtual VObject *GetVObject(); // compatibility
};
//---- abstract class Text ----------------------------------------------
const int cEOT = -1,
cTabw = 40;
const bool cSearchForward= TRUE,
cSearchBackward= !cSearchForward,
cPaginationChange= TRUE,
cNoPaginationChange= !cPaginationChange;
//---- change protocol -------------------------------------------------
// text is an abstract class defining methods to manage a buffer for holding
// text. Together with the text there is a list of robust pointers (called marks)
// into the text. Dependents of the Text classes are notified by
// the observer mechanism
// and receive a change record as described below as argument
enum eTextChanges {
eTextChangedRange,
eTextDeleted,
eTextReplaced // from,to=> replaced range, size=>size of inserted text
};
struct TextChanges {
int from, to, size;
bool pagination;
TextChanges *operator()(int f, int t, int s= 0, bool p= cNoPaginationChange)
{ from= f; to= t; size= s; pagination= p; return this; }
};
//---- Text -----------------------------------------------------------------
class Text: public Object {
public:
MetaDef(Text);
void InitNew();
~Text();
//---- editing
virtual void Insert(byte c, int from, int to);
virtual void Cut(int from, int to);
virtual void Paste(Text *t, int from, int to);
virtual void Copy(Text* save, int from, int to);
void Append(byte c);
void Empty();
// primitive for editing operations
virtual void ReplaceRange(int from, int to, Text *src, int sfrom, int sto);
virtual Text *MakeScratchText(byte *buf, int size);
//---- converting
virtual void InsertStr(int from, int to, byte *str, int len= -1);
virtual void CopyInStr(byte *str, int strSize, int from, int to);
void ReplaceWithStr(byte *str, int len= -1);
char *AsString(); // allocates new buffer !!!
//---- accessing
void SetFString(char *fmt, ...);
Text *Save(int from, int to);
// allocate new text object and copy the given range
void SetDefTab(int tw); // set default tab width
int GetDefTab();
virtual byte& operator[](int i);
virtual int Size();
inline int End();
bool IsEmpty();
virtual int Search(
RegularExp *rex, int *nMatched, int start = 0,
int range= cMaxInt, bool dir= cSearchForward
); // search rex and return position of match (-1 == no match)
//---- style access
virtual void ResetCurrentCharStyle();
virtual void SetCharStyle(TxtCharProp mode, int from, int to, const CharStyleSpec &newStyle);
virtual void SetParaStyle(TxtParaProp mode, int from, int to, const ParaDesc &props);
virtual CharStyle *GetCharStyle(int at);
virtual CharStyle *GetCurrentCharStyle();
virtual ParaStyle *GetParaStyle(int at);
void SetFont(Font *fd, int from= 0, int to= 0);
Font *GetFont(int at= 0);
//---- iterator
virtual class TextIter *MakeIterator(int from=0, int to= cMaxInt); //abstract
//---- text structure access
bool IsParaStart(int at);
bool IsParaEnd(int at);
void GetWordRange(int at, int *start, int *end);
void GetParaRange(int at, int *start, int *end);
static bool IsBreak(byte ch);
static bool IsPara(byte ch);
int FindLastBreak(int from);
int FindNextBreak(int from);
//---- TextPainter
virtual byte *GetLineAccess(byte **buf, int from, int to);
virtual int GetNextFontChange(int at, CharStyle *&sp);
//---- VisualMarks embedding
virtual VisualMark *GetVisualMarkAt(int at);
virtual byte GetMarkChar();
virtual bool IsVisualMark(int at);
//---- Marks
void AddMark(class Mark *);
Mark *RemoveMark(class Mark *);
class Iterator *GetMarkIter();
class MarkList *GetMarkList();
//---- input/output
virtual OStream& PrintOnAsPureText(OStream &s);
virtual IStream& ReadFromAsPureText(IStream &s, long sizeHint= -1);
OStream& PrintOn(OStream&);
IStream& ReadFrom(IStream&);
//---- generic object methods
u_long Hash ();
bool IsEqual(Object *t);
//---- inspecting
void InspectorId(char *b, int s);
//---- static
static bool CheckRange (int max, int from, int to);
protected:
CharStyle *charStyle;
ParaStyle *paraStyle;
Text();
void Init();
bool IsTerminated();
virtual void SetFStringVL(char *fmt, va_list ap);
virtual int GrowSize(int minSize);
// overridden for optimized observing
class Collection *MakeObserverColl();
class Collection *GetObservers();
void DestroyObserverColl();
void SetObserverColl(Collection *);
private:
class MarkList *marks;
class Collection *observers;
int tabWidth;
};
//---- character formatting properties ----------------------------
extern byte TextCharMap[];
const byte cTextPara = 01, // paragraph terminator
cTextBreak = 02; // break
inline bool Text::IsBreak(byte ch)
{
return Iscntrl(ch) && (TextCharMap[ch]&cTextBreak);
}
inline bool Text::IsPara(byte ch)
{
return Iscntrl(ch) && (TextCharMap[ch]&cTextPara);
}
inline int Text::End()
{
return Size()-1;
}
inline bool Text::IsTerminated()
{
return (Size() > 0 && (*this)[Size()-1] == '\0');
}
inline bool Text::CheckRange (int max, int from, int to)
{
return (to > max || from < 0 || from > to) ? FALSE: TRUE;
}
//---- abstract class TextIter ----------------------------------------------
class TextIter {
protected:
int ce, upto, unget;
Text *ct;
public:
TextIter(Text *s, int from= 0, int to= cMaxInt);
virtual ~TextIter(); // abstract
virtual int operator()(int *width= 0, LineDesc* l= 0); // abstract
virtual void Reset(Text *s, int from= 0, int to= cMaxInt);
virtual int Token(int *width, LineDesc* l= 0); // return next token and width, abstract
virtual int GetPos();
virtual Font *FontAt(int);
virtual int GetLastPos(); // get last position
virtual void SetPos(int newPos);
virtual int Unget(); // unget last token
//---- memory allocation
void *operator new (size_t sz)
{ return Storage::ChunkAlloc(sz); }
void operator delete(void *vp)
{ Storage::ChunkFree(vp); }
};
class AutoTextIter {
TextIter *ti;
public:
AutoTextIter(Text *t, int from= 0, int to= cMaxInt);
AutoTextIter(TextIter *tip)
{ ti= tip; }
~AutoTextIter()
{ if (ti) delete ti; }
int operator()(int *width= 0, LineDesc* l= 0)
{ return (*ti)(width, l); }
TextIter *operator->()
{ return ti; }
operator TextIter*()
{ return ti; }
};
#endif